package com.huanghuai.retail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanghuai.retail.entity.SInventory;
import com.huanghuai.retail.entity.Storage;
import com.huanghuai.retail.mapper.SInventoryMapper;
import com.huanghuai.retail.service.SInventoryService;
import com.huanghuai.retail.service.StorageService;
import com.huanghuai.retail.mapper.StorageMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 *名称重复用唯一自定义异常捕获去做
*/
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements StorageService{


    @Resource
    SInventoryMapper sInventoryMapper;

    @Override
    public boolean saveAndUpdate(Storage storage)
    {
        if (storage.getId()==null)
        {return false;}
        LambdaQueryWrapper<Storage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Storage::getId,storage.getId());
        Storage storageInfo = this.getOne(queryWrapper);
        if (storageInfo!=null)
        {
            return this.updateById(storage);
        }
        return this.save(storage);
    }

    @Override
    public boolean removeStorageOne(Long id)
    {
        //        先去查找有没有相关联的 仓库里卖没有任何的商品 就可以删除
        LambdaQueryWrapper<SInventory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SInventory::getStorageId,id);
        SInventory sInventory = sInventoryMapper.selectOne(queryWrapper);
        if (sInventory!= null && sInventory.getNumber()>0 )
        {
            return false;
        }
        return this.removeById(id);
    }

}




